<?php
namespace app\api\controller;

use think\Controller;
use app\api\model\Cart;

class Carts extends Controller
{

    /**
     * 查询购物车内的信息
     */
    public function index()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $userid = input('id');   //用户id
            $cart = new Cart();
            $data = $cart->index($userid);
            if ($data == 0) {
                AjaxReturn('10009', '购物车内暂无信息');
            } else {
                AjaxReturn('1', '成功', ['details' => $data]);
            }
        }
    }

    /**
     * 在购物车中添加新的商品
     */
    public function add()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $info['userid'] = input('id');   //用户id
            $info['issueid'] = input('issueid'); //商品id
            $info['num'] = input('num'); //商品数量
            $info['agencyid'] = input('agencyid'); //店铺id
            $cart = new Cart();
            $data = $cart->add($info);
            if ($data == 1) {
                AjaxReturn('10025', '添加购物车失败');
            } else {
                AjaxReturn('1', '成功', ['details' => '添加购物车成功']);
            }
        }
    }

    /**
     * 删除购物车中的商品
     */
    public function del()
    {
        error_reporting(0);
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $id = input('id');  //购物车id
            $userid = input('userid');
            $cart = new Cart();
            $data = $cart->del($id,$userid);
            if ($data == 0) {
                AjaxReturn('10026', '购物车商品删除失败');
            } else {
                AjaxReturn('1', '成功', ['details' => '购物车商品删除成功']);
            }
        }
    }

    /**
     * 修改购物车中商品的数量
     */
    public function mod()
    {
        if (request()->isGet()){
            AjaxReturn('10020','传值方式错误');
            die;
        }
        if(request()->isPost()) {
            $id = input('id');  //购物车id
            $userid = input('userid'); //用户id
            $num = input('num'); //修改后的商品数量
            $cart = new Cart();
            $data = $cart->mod($id,$userid,$num);
            if ($data == 0) {
                AjaxReturn('10026', '购物车商品修改失败');
            } else {
                AjaxReturn('1', '成功', ['details' => '购物车商品修改成功']);
            }
        }

    }


}